#!/usr/bin/perl

use GDBM_File;

open(FILE,$ARGV[0]);
@lines = <FILE>;
$num = @lines;

for($k = 0; $k < $num; $k++)
{
	if($lines[$k] =~ /=======/)
	{
		chomp($srcline);
		chomp($tgtline);
		chop($srcline);
		chop($tgtline);
	
		@engwords = split(/\s+/,$srcline);
		@hinwords = split(/\s+/,$tgtline);

		foreach $engword (@engwords)
		{
			$counteng{$engword} ++;
		}

		foreach $hinword (@hinwords)
		{
			$counthin{$hinword} ++;
		}

		foreach $engword (@engwords)
		{
			foreach $hinword(@hinwords)
			{
#				print "$engword $hinword\n";
				$countenghin{"$engword $hinword"} ++;
			}
		}
		$srcline = "";
		$tgtline = "";
	}
	else
	{
		($src,$tgt) = split(/\s+/,$lines[$k]);
		$srcline .= $src." ";
		$tgtline .= $tgt." ";
	}
}


print STDERR "Writing it to Disk : $ARGV[1]\n";

dbmopen(%Dice,"$ARGV[1]",0666) || die "can't open";
foreach $key (keys %countenghin)
{
	($engword,$hinword) = split(/\s+/,$key);

	if($counteng{$engword}+$counthin{$hinword}!=0)
    	{ 
#		print $countenghin{"$engword $hinword"}."  ";
#		print $counteng{$engword}."  ".$counthin{$hinword}."\n";
#		print  ((2 * $countenghin{"$engword $hinword"})/($counteng{$engword}+$counthin{$hinword}))."\n"; 
#		$key = $engword."____".$hinword;
		$Dice{"$engword"."____"."$hinword"} = ((2 * $countenghin{"$engword $hinword"})/($counteng{$engword}+$counthin{$hinword})); 
	}
}
dbmclose(%Dice);
print STDERR "Written to Disk\n";

close(ENGFILE);
close(HINFILE);


